#include "Widget.h"
#include "ui_widget.h"
#include<QFileDialog>
#include<QPainter>
#include<QMessageBox>
#include<QKeyEvent>
#include<QDebug>
#include<QTimer>
#include<QMessageBox>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    QMessageBox::information(this,tr(""),tr("点击开始游戏"));
    setWindowTitle("推箱子小游戏———TSY");
    //初始化地图元素
    mPMap=new Gamemap(this);

    mPMap->InitByFile("./Map/lv1.txt");

    //改地图
    mPMap->InitByFile("./Map/lv1.txt");
    mMapPainter = new QPainter(this);//创建painter
    mRole = new Role(this);
    mTimer = new QTimer(this);
    mTimer->start(10);
    setFixedSize(1024,768);

    //定时调用更新函数

    connect(mTimer,&QTimer::timeout,[this](){this->update();});

}

Widget::~Widget()
{
    delete ui;
}


void Widget::paintEvent(QPaintEvent* event)
{

//创建画布
    mMapPainter->begin(this);//设置画布

    //画背景

    mMapPainter->drawImage(QRect(0,0,1024,768),QImage("://imag/backg.png"));


    //画地图
    mPMap->Paint(mMapPainter,QPoint(50,50));


    //画人
    mRole->Paint(mMapPainter,QPoint(50,50));

    mMapPainter->end();
        this->update();
}

void Widget::keyPressEvent(QKeyEvent* event)
{
    switch(event->key())
    {
        case Qt::Key_W:
        case Qt::Key_Up:
        {
            //向上移动
            Collision(-1,0);
            break;
        }
        case Qt::Key_S:
        case Qt::Key_Down:
        {
            //向下移动
            Collision(1,0);
            break;
        }
        case Qt::Key_A:
        case Qt::Key_Left:
        {
            //向左移动
            Collision(0,-1);
            break;
        }
        case Qt::Key_D:
        case Qt::Key_Right:
        {
            //向右移动
            Collision(0,1);
            break;
        }

    }
}
void Widget::Collision(int _dRow,int _dCol)
{
    //判断位置定义
    int newRow =mRole->mRow + _dRow;
    int newCol = mRole->mCol + _dCol;
    if(mPMap->mPArr[newRow][newCol]==Wall)
    {
        return;
    }
    else if(mPMap->mPArr[newRow][newCol]==Box)
    {
        if(mPMap->mPArr[newRow+_dRow][newCol+_dCol]==Road)
        {
            //改变地图元素
            mPMap->mPArr[newRow][newCol]=Road;
            mPMap->mPArr[newRow+_dRow][newCol+_dCol]=Box;
            mRole->Move(_dRow,_dCol);
            qDebug()<<"人物位置："<<mRole->mPaintPos;
        }
        if(mPMap->mPArr[newRow+_dRow][newCol+_dCol]==Point)
        {
            mPMap->mPArr[newRow+_dRow][newCol+_dCol]=InPoint;
            mPMap->mPArr[newRow][newCol]=Road;
            mRole->Move(_dRow,_dCol);
            qDebug()<<"人物位置："<<mRole->mPaintPos;
        }

    }
    else if(mPMap->mPArr[newRow][newCol]==InPoint)
    {
        if(mPMap->mPArr[newRow+_dRow][newCol+_dCol]==Road)
        {
            mPMap->mPArr[newRow+_dRow][newCol+_dCol]=Box;
            mPMap->mPArr[newRow][newCol]=Point;
            mRole->Move(_dRow,_dCol);
            qDebug()<<"人物位置："<<mRole->mPaintPos;
        }
        if(mPMap->mPArr[newRow+_dRow][newCol+_dCol]==Point)
        {
            mPMap->mPArr[newRow+_dRow][newCol+_dCol]=InPoint;
            mPMap->mPArr[newRow][newCol]=Point;
            mRole->Move(_dRow,_dCol);
            qDebug()<<"人物位置："<<mRole->mPaintPos;
        }

    }

//否则移动

    else
    {
        mRole->Move(_dRow,_dCol);
        qDebug()<<"人物位置："<<mRole->mPaintPos;
    }
if(gameover())
   { QMessageBox::information(this,tr(""),tr("恭喜通关"));
    exit(1);
}

}

bool Widget::gameover()
{
    for(int i=0;i<10;i++)
    {
        for(int j=0;j<10;j++)
        {
           if (mPMap->mPArr[i][j]==2)
           return 0;
        }

    }
    return 1;
}

